import { observable, action } from 'mobx';
import { BaseWithList } from 'stores/models/core';
import Motion from './motion';

export default class Motions extends BaseWithList {
  @observable preview = false;

  constructor(props, data) {
    // console.log(props, data);
    super();

    this.init({
      props: {
        parent: props.parent,
        type: 'Motions',
        label: '动效树'
      }
    });

    if (props.motion) { // undefined at start
      props.motion.parent = this;
      this.insert(props.motion);
    }

    if (data) {
      this.insert(new Motion({parent: this}, data));
    }
  }

  @action addNewMotion(firstFrame) {
    this.insert(new Motion({parent: this}, firstFrame));
  }

  @action saveMotions() {
    this.list.forEach(item => {
      this.parent.motions.forEach(motion => {
        if (item.id === motion.id) {
          this.parent.motions.remove(motion);
        }
      });
      this.parent.motions.push(item);
    });
    delete this.parent.temporaryMotions;
  }
}
